package cn.congzhou.crm.web.controller;

import cn.congzhou.crm.domain.Customer;
import cn.congzhou.crm.domain.FormalCustomer;
import cn.congzhou.crm.query.QueryObject;
import cn.congzhou.crm.service.ICustomerService;
import cn.congzhou.crm.service.IFormalCustomerService;
import cn.congzhou.crm.service.ISystemDictionaryItemService;
import cn.congzhou.crm.utils.ResultJSON;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/formalCustomer")
public class FormalCustomerController {

    @Autowired
    private IFormalCustomerService formalCustomerService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")QueryObject qo, Model model) {

        PageInfo<FormalCustomer> pageInfo = formalCustomerService.selectByPage(qo);
        //分页查询
        model.addAttribute("ro",pageInfo);
        //查询所有非正式客户
        //model.addAttribute("customers",formalCustomerService.selectAllCustomer(Customer.STATUS_FORMAL));
        //查询所有课程
        //model.addAttribute("curriculums",systemDictionaryItemService.selectAllCurriculum("curriculum"));

        return "/formalCustomer/list";
    }


    

//  保存客户信息
    @RequestMapping("/save")
    @RequiresPermissions(value = {"添加正式客户","formalCustomer:save"},logical = Logical.OR)
    @ResponseBody
    public Object saveOrUpdate(FormalCustomer formalCustomer) {
        ResultJSON result = new ResultJSON();
        try {
            //添加正式客户
            formalCustomerService.insert(formalCustomer);
            //更新客户状态
            customerService.changeCustomerStatus(formalCustomer.getCustomer().getId(),Customer.STATUS_FORMAL);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("添加成功");
        }

        return result;
    }


    public Object subtractCoursesAmount(){

        return null;
    }


}
